package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.SanphamModel;

import dao.GetCnn;

/**
 * Servlet implementation class tanghang
 */
@WebServlet("/tanghang")
public class tanghang extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public tanghang() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
    	response.setContentType("text/html;charset=UTF-8");
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
		String hotennguoinhan = "";
		String ngaysinh ="";
		String dienthoai = "";
		String diachi = "";
		hotennguoinhan = request.getParameter("tennguoinhanhang");
		ngaysinh = request.getParameter("ngaysinh");
		diachi = request.getParameter("diachi");
		dienthoai = request.getParameter("dienthoai");
		SanphamModel model = new SanphamModel();
		int sodong = model.getTotalRows("select * from DIACHIGIAOHANG");
		String sSQL = "insert into DIACHIGIAOHANG values("+sodong+",'"+hotennguoinhan+"','"+ngaysinh+"','"+diachi+"','"+dienthoai+"')";
		stat.executeUpdate(sSQL);
		request.setAttribute("madcgh", sodong);
		HttpSession session = request.getSession();
		session.setAttribute("madcgh", sodong);
		response.sendRedirect("saveorder");
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
